<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>语料导入</title>
    <!-- 引入 Google Fonts 和图标库 -->
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <!-- 引入 jquery CDN -->
    <script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
    
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Poppins', sans-serif;
        }

        body {
            background: linear-gradient(135deg, #e0f7fa, #f4f4f9);
            min-height: 100vh;
            overflow-x: hidden;
        }

        /* 导航栏优化 */
        .navbar {
            height: 80px;
            background: linear-gradient(45deg, #0084ff, #00c3ff);
            display: flex;
            align-items: center;
            padding: 0 60px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            position: fixed;
            width: 100%;
            z-index: 100;
            gap: 30px;
        }

        .navbar a {
            color: #fff;
            font-size: 1rem;
            font-weight: 500;
            text-decoration: none;
            padding: 12px 20px;
            border-radius: 20px;
            transition: background-color 0.3s ease;
        }

        .navbar a:hover {
            background-color: rgba(255, 255, 255, 0.1);
        }

        .navbar a.active {
            background: rgba(255, 255, 255, 0.15);
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
        }

        /* 主体内容容器 */
        .main-container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 120px 40px 40px;
            min-height: calc(100vh - 80px);
        }

        /* 语料导入模块 */
        .corpus-import {
            background: #fff;
            padding: 30px 40px;
            border-radius: 16px;
            box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
        }

        .corpus-import h1 {
            font-size: 2.2rem;
            font-weight: 600;
            color: #1a1a1a;
            margin-bottom: 25px;
            text-align: left;
        }

        .input-group {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 25px;
        }

        .input-group label {
            flex-basis: 120px;
            text-align: right;
            color: #333;
            font-weight: 500;
        }

        .input-group input[type="text"] {
            padding: 14px 24px;
            border: 2px solid #e0e0e0;
            border-radius: 20px;
            flex-grow: 1;
            font-size: 1rem;
            transition: border-color 0.3s ease;
        }

        .input-group input[type="text"]:focus {
            outline: none;
            border-color: #0084ff;
            box-shadow: 0 0 8px rgba(0, 0, 255, 0.15);
        }

        .file-input-group {
            display: flex;
            align-items: center;
            gap: 20px;
            margin-bottom: 30px;
        }

        .file-input-group label {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 14px 24px;
            background: #f0f4ff;
            border-radius: 20px;
            color: #0084ff;
            font-size: 1rem;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        .file-input-group label:hover {
            background: #e0e8ff;
        }

        .file-input-group input[type="file"] {
            display: none;
        }

        .file-info {
            font-size: 0.9rem;
            color: #666;
        }

        button {
            padding: 14px 32px;
            background: linear-gradient(45deg, #0084ff, #00c3ff);
            color: #fff;
            font-size: 1rem;
            font-weight: 500;
            border: none;
            border-radius: 24px;
            cursor: pointer;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
            transition: transform 0.3s ease;
        }

        button:hover {
            transform: translateY(-2px);
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .navbar {
                padding: 0 20px;
                gap: 15px;
            }

            .navbar a {
                padding: 10px 16px;
                font-size: 0.9rem;
            }

            .main-container {
                padding: 100px 20px 20px;
            }

            .corpus-import h1 {
                font-size: 1.8rem;
            }

            .input-group {
                flex-direction: column;
                align-items: flex-start;
                gap: 10px;
            }

            .input-group label {
                text-align: left;
                flex-basis: auto;
            }
        }
    </style>
</head>

<body>
    <div class="navbar">
        <a href="/MainPagehtml/MainPage.html">首页</a>
        <a href="/analysis">情感分析</a>
        <a href="/MainPagehtml/CorpusImport.html" class="active">语料导入</a>
        <a href="/MainPagehtml/CorpusModify.html">语料修改</a>
        <a href="/MainPagehtml/CorpusQuery.html">语料查询</a>
        <a href="/MainPagehtml/userModelCorpus.html">模型语料</a>
        <!--<a href="/MainPagehtml/ModelShow.html">模型效果</a>-->
    </div>

    <div class="main-container">
        <div class="corpus-import">
            <h1>上传语料文件</h1>

            <!-- 情感标识输入栏 -->
            <div class="input-group">
                <label for="emotionTag">情感标识：</label>
                <input type="text" id="emotionTag" placeholder="如：正面、负面、中性" required>
            </div>

            <div class="file-input-group">
                <label for="corpusFile">
                    <i class="fas fa-file-upload"></i>
                    选择文件
                </label>
                <input type="file" id="corpusFile" accept=".txt,.csv">
                <span class="file-info">支持 .txt 和 .csv 文件</span>
            </div>

            <button onclick="uploadCorpus()">立即上传</button>
        </div>
    </div>

    <script>
        function uploadCorpus() {
            const file = document.getElementById('corpusFile').files[0];
            const emotion = document.getElementById('emotionTag').value.trim();

            if (!emotion) {
                alert('请输入情感标识');
                return;
            }
            if (file) {
                const reader = new FileReader();
                // 读取文件内容（根据文件类型选择读取方式）
                reader.readAsText(file); // 适用于 .txt/.csv 等文本文件
                // 读取完成后获取内容
                reader.onload = function (e) {
                    const fileContent = e.target.result; // 核心：文件内容
                    //写入前后端交互代码
                    $.ajax({
                        method: 'Post',
                        url: '/import',
                        dataType: 'json',
                        contentType: 'application/json;charset=utf-8',
                        data: JSON.stringify({
                            'emotion': emotion,
                            'content': fileContent
                        }),
                        success: function (data) {
                            var result = data.status;
                            if (result === 'success') {
                                alert(`文件上传成功！\n`);
                            } else {
                                alert("文件上传失败！");
                            }
                        }
                    });
                };
                //错误处理
                reader.onerror = function (e) {
                    alert(`文件读取失败！\n`);
                };
                alert(`已选择文件：${file.name}\n 文件情感标识：${emotion}`);
            } else {
                alert('请选择一个语料文件');
            }
        }

        // 导航栏激活状态
        const currentUrl = window.location.href;
        const navLinks = document.querySelectorAll('.navbar a');
        navLinks.forEach(link => {
            if (currentUrl.includes(link.getAttribute('href'))) {
                link.classList.add('active');
            }
        });
    </script>
</body>

</html>
